<template>
  <div class="case-reg-page">
    <div class="title" slot="header">
      <span class="text">线索列表</span>
      <head-btns @load="pageBtns" @add="add" @mod="mod" @del="del"/>
    </div>
    <list-table ref="listTable" @selected="listSelected"/>
  </div>
</template>
<script>
  import detailInfo from "./detail-info";
  import discretionApi from "@/api/system/discretion";
  import caseApi from "@/api/system/case";
  import caseclueApi from "@/api/system/caseclue";
  import utils from "@/assets/utils/zhzf-util";
  import dictCache from "@/assets/utils/cache-dict-util";
  import headBtns from "@/views/comps/zhzf/head-btns";
  import listSearch from "@/views/comps/zhzf/list-search";
  import { mapState, mapMutations } from "vuex";
  import { frameInRoutes } from "@/router/routes";
  import layoutHeaderAside from "@/views/layout/header-aside";
  import ListSearch from "../../comps/zhzf/list-search";
  import ListTable from "@/views/comps/zhzf/list-table";
  import tabPageMinxin from "@/assets/minxin/tag-page";
  import detailView from "@/views/evidence/register/detail-info";
  import CustBtnCol from '@/views/comps/zhzf/list-table-col-btn';
  export default {
    components: { headBtns, detailInfo,ListSearch,ListTable,CustBtnCol},
    mixins: [tabPageMinxin],
    data() {
      return {
        dataList: [],
        rowHandle: {},
        columns: [
          // { title: "流水号", key: "caseClueId\n", width: 180, sortable: "true" },//根据流水号来进行修改
          { title: "立案地点", key: "fillingPlace", sortable: "true",width:200},
          {
            title: "投诉日期 ",
            key: "complainTime",
            width: 200,
            sortable: "true",
            formatter: (row, col, val) => {
              return utils.formatDts(val, "YYYY-MM-DD HH:mm:ss");
            }
          },
          { title: "投诉来源", key: "complainSource", width: 200, sortable: "true" },
          { title: "投诉人", key: "complianPerson", width: 150, sortable: "true" },
          {
            title: "车牌号 ",
            key: "carNo",
            width: 80,
            sortable: "true",
          },
          {
            title: "发生地点 ",
            key: "occurAddress",
            width: 150,
            sortable: "true",
          },
          { title: "发生时间 ", key: "occurTime", width: 120, sortable: "true",
            formatter: (row, col, val) => {
              return utils.formatDts(Number(val), "YYYY-MM-DD HH:mm:ss");
            }
          },
          { title: "办理期限", key: "limitTime", width: 80, sortable: "true",
            formatter: (row, col, val) => {
              return utils.formatDts(Number(val), "YYYY-MM-DD HH:mm:ss");
            }
          },
          { title: "办理机构", key: "orgNo", width: 80, sortable: "true" },
          {
            title: '操作',
            key: 'op',
            width:200,
            component: {
              name: CustBtnCol,
              props: {
                items: [
                  {text: '修改', emit: 'mod'},
                  {text: '删除', emit: 'del'}
                ]
              }
            }
          }
        ],
        options: {
          stripe: true,
          highlightCurrentRow: true
        },
        pagination: {
          currentPage: 1,
          pageSize: 15,
          total: 0
        },
        queryParam: {},
        selected: [],
        isSignal:false
      };
    },
    computed: {
      ...mapState("d2admin/user", ["info"])
    },
    mounted() {
      this.$bus.$off('mod').$on('mod', res => {
        this.selected = [res];
        this.mod();
      });
      this.$bus.$off('del').$on('del', res => {
        this.selected = [res];
        this.del();
      });
      this.initPage();
    },
    methods: {
      ...mapMutations({
        pageInit: "d2admin/page/init",
        headerSet: "d2admin/menu/headerSet"
      }),
      listSelected(list) {
        this.selected = list;
      },
      pageBtns(btns) {
        btns = btns || [];
        this.rowHandle = {
          width: 80,
          custom: []
        };
        btns.forEach(item => {
          this.rowHandle.custom.push({
            text: item.name,
            size: "mini",
            emit: `${item.url}-model`,
            icon: item.icon,
            style: `color:${item.url == "del" ? "#f53c1b" : "#1B98F5"}`
          });
        });
        this.rowHandle.custom.splice(0,1);
        this.rowHandle.custom.push({
          text: "详情",
          size: "mini",
          emit: "edit-model",
          style: "color:#409eff;text-decoration:underline;font-size:14px;"
        })
      },

      async sortChange(column, prop, order) {
        //根据表头参数进行排序
        // await this._fetchListData();
      },
      rowClick(row) {
        let els = this.$refs.d2Crud.$children;
        this.$refs.d2Crud.$children[els.length == 1 ? 0 : els.length - 2].toggleRowSelection(row);
      },
      selectChange(selection) {
        this.selected = selection;
      },
      register(rd) {
        if (rd && rd.row) {
          this.registers(rd.row);
        } else if (this.selected.length == 1) {
          this.registers(this.selected[0]);
        } else {
          utils.showTips('请选择一行数据', false)
          return;
        }
      },
      async registers(row ) {
        //立案事件
        this.$confirm("确认立案？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "确定",
          cancelButtonText: "取消"
        })
                .then(async () => {
                  await this.caseRegister(row);
                  await this._fetchListData();
                })
                .catch(() => {});
      },
      async casetypechange() {
        await this._fetchListData();
      },
      async caseRegister(row) {
        let resp = await caseApi.caseRegister({
          id: row.caseAccessId,
          vars: { caseCreateUser: this.info.username , processKey:'xingyang_trafficeCase_handle'}
        }); //启动流程
        if (resp) {
          let ok = resp.ok;
          let taskId = resp.data.taskId
          if (ok) {
            let res = await caseApi.caseExamine({//立案流程
              taskId: taskId,
              commentMessage: "11111",
              commentType: "",
              vars: {}
            });
            if (res.ok) {
              utils.showTips(res.msg, res.ok);
            }
          }
        }
      },
      async initPage() {
        // await this._fetchListData();
        let param = {
          endStatus:'登记'
        }
        await this.$refs.listTable.initPage({
          method: caseclueApi.pageList,
          cols: this.columns,
          param:param,
        })
      },
      async _fetchListData() {
        let param = {};
        param.caseProcessStarted = "false";
        let res = await caseApi.pageList({ ...this.pagination, ...param });
        this.dataList = res.data.rows;
        if (res.status != 0) {
          utils.showTips("案件添加失败！");
        }
        this.pagination.total = res.data.total || this.dataList.length;
      },
      pageChange(currentPage) {
        this.pagination.currentPage = currentPage;
        // this._fetchListData();
      },
      showDetail(row) {
        let pageTitle = "线索登记-录入";
        let rpath = "evidence/register/detail";
        let rname = "eveidenceRegDetail";
        this.addTabPage(pageTitle, rpath, rname, detailView, { row: row });
      },
      add() {
        //登记跳路由
        this.showDetail();
      },
      mod(rd) {
        if (rd&&rd.row) {
          this.showDetail(rd.row);
        } else if (this.selected.length == 1) {
          this.showDetail(this.selected[0]);
        } else {
          utils.showTips("请选择一行数据");
          return;
        }
      },
      del() {
        if (!this.selected.length) {
          utils.showTips("请选择要删除的数据", false);
          return;
        }
        this.$confirm("确认删除？", "确认信息", {
          distinguishCancelAndClose: true,
          confirmButtonText: "删除",
          cancelButtonText: "取消"
        }).then(async () => {
          let delIds = [];
          this.selected.forEach(item => {
            delIds.push(item.caseClueId);
          });
          let res = await caseclueApi.del(delIds);
          if (res.ok) {
            utils.showTips(res.msg);
          }
          this.initPage()
        });
      },
    }
  };
</script>
<style lang="scss">
  .case-reg-page {
    .d2-crud-body {
    }
  }
</style>
